AddCSLuaFile( "cl_init.lua" );
AddCSLuaFile( "shared.lua" );

include( "shared.lua" );

function ENT:Initialize()
	
	self:SetModel( self.Model );
	self:PhysicsInit( SOLID_VPHYSICS );
	self:SetMoveType( MOVETYPE_VPHYSICS );
	self:SetSolid( SOLID_VPHYSICS );
	
	local phys = self:GetPhysicsObject();
	if( phys and phys:IsValid() ) then
		
		phys:Wake();
		
	end
	
	self.NextThinkTime = 0;
	
	self:SetUseType( SIMPLE_USE );
	
	self:SetNWBool( "O", false );
	C.ClearLinks( self );
	
end

function ENT:SetActive( on )
	
	self:SetNWBool( "O", on );
	
	if( on ) then
		
		self:EmitSound( Sound( "ambient/machines/spinup.wav" ) );
		
	else
		
		self:EmitSound( Sound( "ambient/machines/spindown.wav" ) );
		
	end
	
end

function ENT:ToggleActive()
	
	self:SetActive( !self:GetNWBool( "O" ) );
	
end

function ENT:Use( act, call, type, val )
	
	self:ToggleActive();
	
end

function ENT:CheckLinks()
	
	for k, v in pairs( self.Links ) do
		
		if( !v or !v:IsValid() ) then
			
			table.remove( self.Links, k );
			continue;
			
		end
		
		if( v:GetPos():Distance( self:GetPos() ) > 1024 ) then
			
			table.remove( self.Links, k );
			
			self:EmitSound( Sound( "weapons/stunstick/spark" .. tostring( math.random( 1, 3 ) ) .. ".wav" ) );
			
		end
		
	end
	
end

function ENT:AccessResource( res, amt )
	
	for _, v in pairs( self.Links ) do
		
		if( v.CosmicType == CE_CONTAINER and v:GetAmount( res ) > ( amt or 0 ) ) then
			
			return true;
			
		end
		
	end
	
	return false;
	
end

function ENT:GetContainer( res )
	
	for _, v in pairs( self.Links ) do
		
		if( v.CosmicType == CE_CONTAINER and v:GetAmount( res ) > 0 ) then
			
			return v;
			
		end
		
	end
	
end

function ENT:CanGenerate()
	
	return true;
	
end

function ENT:OnGenerate()
	
	
	
end

function ENT:Think()
	
	self:CheckLinks();
	
	if( self:GetNWBool( "O" ) and self:CanGenerate() ) then
		
		if( CurTime() >= self.NextThinkTime ) then
			
			self:OnGenerate();
			local scrubEnt = nil;
			
			for _, v in pairs( self.Links ) do
				
				if( ( v.CosmicType == CE_DISTRIBUTOR or v.CosmicType == CE_BIGDISTRIBUTOR ) and v.Resources[self.Resource] and v:GetNWBool( "O" ) ) then
					
					local scrubbed = false;
					
					for _, n in pairs( self.Links ) do
						
						if( n.CosmicType == CE_CONTAINER and !n:AtMax( self.Resource ) and self:AccessResource( RES_ENERGY, v.Resources[self.Resource] * 2 ) ) then
							
							scrubbed = true;
							scrubEnt = v;
							n:AddResource( self.Resource, v.Resources[self.Resource] );
							self:GetContainer( RES_ENERGY ):RemoveResource( RES_ENERGY, v.Resources[self.Resource] * 2 );
							break;
							
						end
						
					end
					
					if( scrubbed ) then
						
						break;
						
					end
					
				end
				
			end
			
			if( scrubEnt and scrubEnt:IsValid() ) then
				
				self.NextThinkTime = CurTime() + scrubEnt.Delay;
				
			else
				
				self.NextThinkTime = CurTime() + 0.5;
				
			end
			
		end
		
	elseif( self:GetNWBool( "O" ) and !self:CanGenerate() ) then
		
		self:SetActive( false );
		
	end
	
end